<?php

namespace app\api\service;

use app\api\service\EpayCore as ServiceEpayCore;
use app\common\EpayCore;
use Exception;
use think\facade\Db;

class Epay extends Pay
{
    public function __construct()
    {
        parent::__construct();
    }
    public function Epayconfig()
    {
        $conf = getConfig('admin', 'sys_pay');
        // 易支付
        $epay_config['apiurl'] = $conf['epayConf']['apiUrl'];
        //商户ID
        $epay_config['pid'] =   $conf['epayConf']['pid'];
        //商户密钥
        $epay_config['key'] =    $conf['epayConf']['key'];

        return $epay_config;
    }

    public function EpayCreateOrder($param)
    {
        $epay_config = $this->Epayconfig();
        if ($epay_config['apiurl'] == '' || $epay_config['pid'] == '' ||  $epay_config['key']  == '') return error('易支付配置异常，请联系管理处理');
        //构造要请求的参数数组，无需改动
        $parameter = array(
            "pid" => $epay_config['pid'],
            "type" => $param['type'],
            "notify_url" => $this->notifyUrl,
            "return_url" => $this->returnUrl,
            "out_trade_no" => $this->tradoNo,
            "name" => $param['name'],
            "money"    => $param['money'],
        );
        //建立请求
        $epay = new ServiceEpayCore($epay_config);
        $payLink = $epay->getPayLink($parameter);
        $param = ['user_id' => $param['user_id'], 'call_type' => 1, 'good_id' => $param['good_id'], 'good_type' => $param['good_type'], 'good_name' => $param['name'], 'pay_type' => $param['type'],  'out_trade_no' => $this->tradoNo, 'money' => $param['money'], 'ip' => Request()->ip(), 'status' => 0, 'create_time' => time()];
        if (Db::name('order')->insert($param)) {
            return ['payLink' => $payLink];
        }
        throw new Exception('订单生成失败');
    }
    public function orderStatus($out_trade_no)
    {
        $epay_config = $this->Epayconfig();
        return (new ServiceEpayCore($epay_config))->orderStatus($out_trade_no);
    }
    // 异步回调
    public function EpaynotifyUrl()
    {

        $epay_config = $this->Epayconfig();
        //计算得出通知验证结果
        $epay = new ServiceEpayCore($epay_config);
        $verify_result = $epay->verifyNotify();
        if ($verify_result) { //验证成功
            //商户订单号
            $out_trade_no = $_GET['out_trade_no'];
            //彩虹易支付交易号
            $trade_no = $_GET['trade_no'];
            //交易状态
            $trade_status = $_GET['trade_status'];
            //支付方式
            $type = $_GET['type'];
            //支付金额
            $money = $_GET['money'];
            try {
                $payStatus = $epay->orderStatus($trade_no);
                if ($payStatus) {
                    $order =  Db::name('order')->where('out_trade_no', $out_trade_no)->find();
                    $order['trade_no'] = $trade_no;
                    if ($order['status'] == 0) {
                        return $this->callReward($order);
                    } else {
                        $this->log(0, '订单重复');
                        return false;
                    }
                } else {
                    Db::name('order')->where('out_trade_no', $out_trade_no)->update(['status' => 2, 'trade_no' => $trade_no]);
                    $this->log(0, '验证失败');
                }
            } catch (Exception $e) {
                $this->log(0, $e->getMessage());
            }
        } else {
            $this->log(0, '验证失败');
        }
        return false;
    }
    public function EpayReturnUrl()
    {
        $epay_config = $this->Epayconfig();
        //计算得出通知验证结果
        $epay = new ServiceEpayCore($epay_config);
        $verify_result = $epay->verifyReturn();
        if ($verify_result) { //验证成功
            //商户订单号
            $out_trade_no = $_GET['out_trade_no'];
            //支付宝交易号
            $trade_no = $_GET['trade_no'];
            //交易状态
            $trade_status = $_GET['trade_status'];
            //支付方式
            $type = $_GET['type'];
            $payStatus = $epay->orderStatus($trade_no);
            if ($payStatus) {
                $order =  Db::name('order')->where('out_trade_no', $out_trade_no)->find();
                return ['code' => 200, 'msg' => '支付成功'];
            } else {
                return ['code' => 0, 'msg' => '订单不存在'];
            }
        } else {
            //验证失败
            $this->log(0, '验证失败');
            return ['code' => 0, 'msg' => '验证失败'];
        }
    }
}
